package com.xmg.nio;

import org.junit.Test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class BufferTest {
    public static void main(String[] args) {
        ByteBuffer buf = ByteBuffer.allocate(1024);

        CharBuffer charBuffer = CharBuffer.allocate(12);

        IntBuffer intBuffer = IntBuffer.allocate(100);
        intBuffer.put(new int[]{1, 2, 3, 4});
        System.out.println(intBuffer.position());
        System.out.println(intBuffer.limit());
        System.out.println(intBuffer.capacity());
        System.out.println(intBuffer.mark());

        intBuffer.flip();
        System.out.println(intBuffer.position());
        System.out.println(intBuffer.limit());
        System.out.println(intBuffer.capacity());
        System.out.println(intBuffer.mark());

//        intBuffer.put(new int[]{6,7,8,9,});
//        intBuffer.rewind();
//        System.out.println(intBuffer.position());
//        System.out.println(intBuffer.limit());
//        System.out.println(intBuffer.capacity());
//        System.out.println(intBuffer.mark());

        System.out.println(intBuffer.get(5));
    }

    //通道之间的数据传输
    @Test
    public void test2() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("f:/Monitor2.0.zip"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("f:/Monitor3.0.zip"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW);

//        inChannel.transferTo(0,inChannel.size(),outChannel);
        outChannel.transferFrom(inChannel,0,inChannel.size());
        inChannel.close();
        outChannel.close();
    }
    //直接缓冲区完成文件复制
    @Test
    public void test1() throws IOException {

        FileChannel inChannel = FileChannel.open(Paths.get("f:/Monitor2.0.zip"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("f:/Monitor3.0.zip"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW);

        MappedByteBuffer inMap = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer outMap = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

        byte[] dst = new byte[inMap.limit()];
        inMap.get(dst);
        outMap.put(dst);

        inChannel.close();
        outChannel.close();
    }

    @Test
    public void readHttpImg() throws IOException {

        URL url = new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi2.hdslb.com%2Fbfs%2Farchive%2Fff4faf3b6d12924ea89f6cfcff5a073cdce2acfe.jpg&refer=http%3A%2F%2Fi2.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1614588176&t=149f8177a97dff3ed13cc0e0a600b310");
        ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        FileChannel open = FileChannel.open(Paths.get("d:/t-ara.png"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        int len = 0;
        while ((len = readableByteChannel.read(buffer))!=-1){
            buffer.flip();
            open.write(buffer);
            buffer.clear();
        }

        open.close();
        readableByteChannel.close();
    }

}
